using System;
using System.Text;
using System.Reflection;

namespace OST.Shared.SharedTypes
{
    /// <summary>
    /// summing-up the configurations
    /// </summary>
    public class Configuration
    {
        #region CTOR

        public Configuration()
        {
            Name = string.Empty;
            Vers = 0;
            Logging = new LoggingConfiguration();
            Location = new LocationConfiguration();
            TimeService = new TimeServiceConfiguration();
            Frame = new FrameConfiguration();
            Motor = new MotorConfiguration();
            Ethernet = new EthernetConfiguration();
        }
        #endregion

        #region Properties

        /// <summary>
        /// name of this open-sun-tracker instance
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// version of this instance
        /// </summary>
        public int Vers { get; set; }

        /// <summary>
        /// logging configuration property
        /// </summary>
        public LoggingConfiguration Logging { get; set; }

        /// <summary>
        /// location configuration property
        /// </summary>
        public LocationConfiguration Location { get; set; }

        /// <summary>
        /// time-service (ntp) configuration property
        /// </summary>
        public TimeServiceConfiguration TimeService { get; set; }

        /// <summary>
        /// frame configuration property
        /// </summary>
        public FrameConfiguration Frame { get; set; }

        /// <summary>
        /// motor configuration property
        /// </summary>
        public MotorConfiguration Motor { get; set; }

        /// <summary>
        /// ethernet configuration property
        /// </summary>
        public EthernetConfiguration Ethernet { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// evualting property to validate configurtion
        /// </summary>
        public void Validate()
        {
            //check version
            var assMajorVersionValue = Assembly.GetExecutingAssembly().GetName().Version.Major;

            if (assMajorVersionValue != Vers)
                throw new OSTException("config-version incompatible, expected '" 
                    + assMajorVersionValue + "', real '" + Vers + "'");

            Logging.Validate();
            Location.Validate();
            TimeService.Validate();
            Frame.Validate();
            Motor.Validate();
            Ethernet.Validate();
        }

        public override string ToString()
        {
            var str = new StringBuilder();

            str.AppendLine("Name: " + Name);
            str.AppendLine("Vers: " + Vers);
            str.AppendLine("Logging: " + Logging);
            str.AppendLine("Location: " + Location);
            str.AppendLine("TimeService: " + TimeService);
            str.AppendLine("Frame: " + Frame);
            str.AppendLine("Motor: " + Ethernet);
            str.AppendLine("Ethernet: " + Ethernet);

            return str.ToString();
        }

        #endregion

    }
}
